package com.xuelangyun.form.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuelangyun.form.common.log.annotation.SysLog;
import com.xuelangyun.form.common.log.annotation.SysModule;
import com.xuelangyun.form.common.utils.PageUtils;
import com.xuelangyun.form.common.utils.R;
import com.xuelangyun.form.sys.entity.SysDictEntity;
import com.xuelangyun.form.sys.service.ISysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 数据字典
 *
 * @author weiqing.hk
 * @date 2019-03-20 10:54:34
 */
@RestController
@RequestMapping("sys/sysdict")
@Api(value = "字典表-通用", tags = {"字典表-通用"})
public class SysDictMgController {
    @Autowired
    private ISysDictService iSysDictService;

    /**
     * 类型列表
     */
    @GetMapping("/types")
    @ApiOperation(httpMethod = "GET", value = "字典表类型列表信息")
    // @RequiresPermissions("sys:sysdict:list")
    public R<PageUtils> types(String type, PageUtils page) {
        PageUtils pageData = iSysDictService.queryTypePage(type, page);
        return R.ok(PageUtils.class).setData(pageData);
    }

    @SuppressWarnings("rawtypes")
    @GetMapping("/findByType")
    @ApiOperation(httpMethod = "GET", value = "按照类型查询字典表信息")
    public R<List> findByType(String type) {
        List<SysDictEntity> list = iSysDictService.findByType(type);
        return R.ok(List.class).setData(list);
    }

    @GetMapping("/findByTypeValue")
    @ApiOperation(httpMethod = "GET", value = "按照类型和值查询字典表信息")
    public R<SysDictEntity> findByTypeValue(String type, String value) {
        SysDictEntity data = iSysDictService.findByTypeDictValue(type, value);
        return R.ok(SysDictEntity.class).setData(data);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(httpMethod = "GET", value = "字典表列表信息")
    // @RequiresPermissions("sys:sysdict:list")
    public R<PageUtils> list(SysDictEntity entity, PageUtils page) {
        PageUtils pageData = iSysDictService.queryPage(entity, page);
        return R.ok(PageUtils.class).setData(pageData);
    }

    /**
     * 列表
     */
    @GetMapping("/getAll")
    @ApiOperation(httpMethod = "GET", value = "字典表全部信息")
    // @RequiresPermissions("sys:sysdict:list")
    public R<Object> getAll(SysDictEntity entity, PageUtils page) {
        List<SysDictEntity> list = iSysDictService.list();
        return R.ok().setData(list);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{dictId}")
    @ApiOperation("查询字典表详情")
    public R<SysDictEntity> info(@PathVariable("dictId") String dictId) {
        SysDictEntity sysDict = iSysDictService.getById(dictId);
        return R.ok(SysDictEntity.class).setData(sysDict);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(httpMethod = "POST", value = "保存字典表信息")
    @SysLog(value = "RequiresPermissions保存字典表信息", system = SysModule.sys)
    @RequiresPermissions("sys:sysdict:save")
    public R<Object> save(@RequestBody SysDictEntity sysDict) {
        iSysDictService.save(sysDict);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(httpMethod = "POST", value = "修改字典表信息")
    @SysLog(value = "修改字典表信息", system = SysModule.sys)
    @RequiresPermissions("sys:sysdict:update")
    public R<Object> update(@RequestBody SysDictEntity sysDict) {
        iSysDictService.updateById(sysDict);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/updateType")
    @ApiOperation(httpMethod = "POST", value = "修改字典表信息")
    @SysLog(value = "修改字典表信息", system = SysModule.sys)
    @RequiresPermissions("sys:sysdict:update")
    public R<Object> updateType(@RequestBody SysDictEntity sysDict) {
        iSysDictService.updateTypeByEntity(sysDict);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(httpMethod = "POST", value = "删除字典表信息")
    @RequiresPermissions("sys:sysdict:delete")
    @SysLog(value = "删除字典表信息", system = SysModule.sys)
    public R<Object> delete(@RequestBody String[] dictIds) {
        iSysDictService.removeByIds(Arrays.asList(dictIds));
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/deleteType")
    @ApiOperation(httpMethod = "POST", value = "删除字典表信息")
    @RequiresPermissions("sys:sysdict:delete")
    @SysLog(value = "删除字典表信息", system = SysModule.sys)
    public R<Object> deleteType(@RequestBody Map<String, String> map) {
        if (null == map || map.isEmpty() || !map.containsKey("type")) {
            return R.error("type can't be null.");
        }
        String type = map.get("type");
        QueryWrapper<SysDictEntity> query = new QueryWrapper<>();
        query.eq("type", type);
        boolean remove = iSysDictService.remove(query);
        return R.ok(String.valueOf(remove));
    }

}
